/*!
\file:PCB.h
\author:linlin 
\version:3.0
\date:2013/9/30
\brief:
\details:
*/

#include <reg52.h>
#include <rtx51tny.h>
#include "PCB.h"

#define INIT       0                   /* task number of task:  init                */
#define TASK1      1                   /* task number of task:  task1               */
#define TASK2      2                   /* task number of task:  task2               */
#define TASK3      3                   /* task number of task:  task3               */

#define CLK_SXF_QF_BJ 1
#define CLK_SXF 1
#define CLK_QF	1
#define CLK_BJ  1
#define CLK_YF_YB 2
#define CLK_YF  2
#define CLK_YB  2
#define INTERVAL_TIME_P 50 //10*50ms
#define INTERVAL_TIME_N 50 //10*50ms

void out_clk(unsigned char n)
{
	if(n==1)
	{
		out_clk1=0;
		out_clk1=1;
		out_clk1=0;
	}
	else
	{
		out_clk2=0;
		out_clk2=1;
		out_clk2=0;
	}
}

void init(void) _task_ INIT
{
	/*Initialize IO pin*/
	P0=0xff;//11111111b,yd
	P1=0xff;//11111111b,yg
	P2=0x00;//00000000b,out
	P3=0x3f;//00111111b,an
	
	for(;;)
	{
		sxf_qf_bj=0x80;
		out=sxf_qf_bj;
		out_clk(CLK_QF);
		yf_yb=0;
		out=yf_yb;
		out_clk(CLK_YF);
		out_oe=0;
	
		os_create_task(TASK1);
		os_create_task(TASK2);
		os_create_task(TASK3);
		os_wait(K_SIG,0,0);
	}
}

void task1(void) _task_ TASK1//DX & MP
{
	for(;;)
	{
		if((in_dxg==DX_SIG)||(in_zyg==MP_SIG))
		{
			yf_yb=0;
			out=yf_yb;
			out_clk(CLK_YF_YB);
			sxf_qf_bj=0;
			if(in_dxg==DX_SIG) bj=1;
			else					qf=bj=1;
			out=sxf_qf_bj;
			out_clk(CLK_SXF_QF_BJ);
			
			os_delete_task(TASK2);
			os_delete_task(TASK3);
			os_wait(K_TMO,500,0);
			if((in_dxg!=DX_SIG)&&(in_zyg!=MP_SIG))
			{
				os_send_signal(INIT);
			}
		}
		else os_switch_task();
	}
}

void task2(void) _task_ TASK2//YD
{
	for(;;)
	{
		if((P0&0x3f)!=0x3f)
		{
			if(in_yd1==0) yf1=1;
			else				  yf1=0;
			if(in_yd2==0) yf2=1;
			else					yf2=0;
			if(in_yd3==0) yf3=1;
			else					yf3=0;
			if(in_yd4==0) yf4=1;
			else					yf4=0;
			if(in_yd5==0) yf5=1;
			else					yf5=0;
			if(in_yd6==0) yf6=1;
			else					yf6=0;
			yb=1;
			out=yf_yb;
			out_clk(CLK_YB);
			os_wait(K_TMO , INTERVAL_TIME_P , 0);
			yb=0;
			out=yf_yb;
			out_clk(CLK_YB);
			os_wait(K_TMO , INTERVAL_TIME_N , 0);
		}
		else
		{
			yf_yb=0;
			out=yf_yb;
			out_clk(CLK_YF_YB);
			os_switch_task();
		}
	}
}

void task3(void) _task_ TASK3//YG & AN
{
	for(;;)
	{
		if(((P1&0x3f)!= 0)||((P3&0x3f)!=0x3f))
		{
			if((in_an1==0)&&(in_yd1!=0)||(in_yg1==1)) sxf1=1;
			else	 						 			 		   						sxf1=0;
			if((in_an2==0)&&(in_yd2!=0)||(in_yg2==1)) sxf2=1;
			else	 						 			  		 						sxf2=0;
			if((in_an3==0)&&(in_yd3!=0)||(in_yg3==1)) sxf3=1;
			else	 						 			  		 						sxf3=0;
			if((in_an4==0)&&(in_yd4!=0)||(in_yg4==1)) sxf4=1;
			else	 						 			  		 						sxf4=0;
			if((in_an5==0)&&(in_yd5!=0)||(in_yg5==1)) sxf5=1;
			else	 						 			  		 						sxf5=0;
			if((in_an6==0)&&(in_yd6!=0)||(in_yg6==1)) sxf6=1;
			else	 						 			  		 						sxf6=0;
			out=sxf_qf_bj;
			out_clk(CLK_SXF);
			os_wait(K_TMO,10,0);
		}
		else 
		{
			sxf_qf_bj=0x80;
			out=sxf_qf_bj;
			out_clk(CLK_SXF_QF_BJ);
			os_switch_task();
		}
	}
}


